package com.nh.action;

import java.sql.Timestamp;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.nh.entity.GKLUser;

public class GKLUserAction extends BaseAction {
	private GKLUser user;
	private Long optionId;

	/**
	 * 用户列表
	 * 
	 * @return
	 */
	public String userList() {
		Criteria criteria = this.createCriteria(GKLUser.class);
		// criteria.addOrder(Order.desc("publishTime"));
		criteria.addOrder(Order.desc("createTime"));
		this.queryByPage(criteria, "gKLUserAction!userList.action", 10);
		return "userList";
	}

	public String userAdd() {
		user = new GKLUser();
		user.setCreateTime(new Timestamp(System.currentTimeMillis()));
		return "userAdd";
	}

	public String saveUser() {
		if (null==user.getId()) {
			user.setCreateTime(new Timestamp(System.currentTimeMillis()));
			this.getBaseService().save(user);
		} else {
			user.setCreateTime(new Timestamp(System.currentTimeMillis()));
			this.getBaseService().saveOrUpdate(user, GKLUser.class);
		}

		return userList();
	}

	/**
	 * 锁定
	 * 
	 * @return
	 */
	public String lockUser() {
		GKLUser temp = this.getBaseService().findById(optionId, GKLUser.class);
		temp.setUserState(1);
		temp.setCreateTime(new Timestamp(System.currentTimeMillis()));
		this.getBaseService().saveOrUpdate(temp, GKLUser.class);
		return userList();
	}

	/**
	 * 解锁
	 * 
	 * @return
	 */
	public String unlockUser() {
		GKLUser temp = this.getBaseService().findById(optionId, GKLUser.class);
		temp.setUserState(0);
		temp.setCreateTime(new Timestamp(System.currentTimeMillis()));
		this.getBaseService().saveOrUpdate(temp, GKLUser.class);
		return userList();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String removeUser() {
		GKLUser temp = this.getBaseService().findById(optionId, GKLUser.class);
		temp.setUserState(0);
		this.getBaseService().delete(temp);
		return userList();
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String modifyUser() {
		user = this.getBaseService().findById(optionId, GKLUser.class);
		return "userAdd";
	}

	public GKLUser getUser() {
		return user;
	}

	public void setUser(GKLUser user) {
		this.user = user;
	}

	public Long getOptionId() {
		return optionId;
	}

	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

}
